package com.murray.assignment.controller;

import com.murray.assignment.mapper.CartMapper;
import com.murray.assignment.pojo.Cart;
import com.murray.assignment.pojo.CartInfo;
import com.murray.assignment.pojo.Users;
import com.murray.assignment.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * @author Murray
 * @Date 2021/8/4
 * @Time 8:57
 * @Description 有关购物车的操作
 *
 */
@Controller
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @RequestMapping(value = "/addGoodsToCart")
    @ResponseBody
    public Integer addGoodsToCart(@RequestParam(value = "skuId") Integer skuId, @RequestParam(value = "num") Integer num, HttpSession session) {
        //代表session中有用户
        if (session.getAttribute("user") != null) {
            Users users = (Users) session.getAttribute("user");
            Integer cartId = users.getCart().getCartId();
            CartInfo cartInfo = new CartInfo();
            cartInfo.setCartId(cartId);
            cartInfo.setBuyNum(num);
            cartInfo.setSkuId(skuId);
            cartService.addGoodsToCart(cartInfo);
            Integer totalNum = cartService.returnGoodsNums(cartId);
            Cart cart = users.getCart();
            cart.setTotalNum(totalNum);

            //将更新后的user重新存回去，更新了
            //user中的cart信息
            session.setAttribute("user", users);

            return totalNum;

        }
        return -1;
    }

    /**
     * @return
     * @Description 显示购物车的详细信息
     */
    @RequestMapping("/showCartDetails")
    public ModelAndView showCartDetails(HttpSession session) {

        if (session.getAttribute("user") != null) {

            //获得cartId
            Users users = (Users) session.getAttribute("user");
            Integer cartId = users.getCart().getCartId();
            System.out.println(cartId);
            //传到service层处理
            Cart cart = cartService.selectCartDetails(cartId);
            System.out.println(cart);
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.setViewName("shopCart");
            modelAndView.addObject("cart", cart);
            return modelAndView;
        } else {
            //没登录去登录
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.setViewName("login");
            return modelAndView;
        }
    }

    @RequestMapping("/deleteGoodsFromCart")
    @ResponseBody
    public Integer deleteGoodsFromCart(@RequestParam(value = "skuId") Integer skuId, HttpSession session) {


        if (session.getAttribute("user") != null) {
            //获得购物车id
            Users users = (Users) session.getAttribute("user");
            Integer cartId = users.getCart().getCartId();
            CartInfo cartInfo = new CartInfo();
            cartInfo.setCartId(cartId);
            cartInfo.setSkuId(skuId);
            //拿到的是购物车中商品总数
            Integer totalNum = cartService.deleteGoodsFromCart(cartInfo);
            users.getCart().setTotalNum(totalNum);
            session.setAttribute("user", users);
            return totalNum;
        }
        return -1;


    }


}
